package com.ncloud.system.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ncloud.common.body.Result;
import com.ncloud.common.utils.ResultUtil;
import com.ncloud.system.entity.Company;
import com.ncloud.system.service.impl.CompanyServiceImpl;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created by gaowei on 2017/7/27.
 */
@RestController
@RequestMapping("/sys/company")
public class CompanyController {
	@Autowired
	private CompanyServiceImpl companyService;
	@GetMapping(value = "/alllist")
    public Result<?> getCompanyAllList(HttpServletRequest request){
    	JSONObject params = new JSONObject();
    	params.put("state", 2);//state为2 查询所有的企业 包括锁定和未锁定
    	JSONArray infos = companyService.getCompanyInfo(params);
		return ResultUtil.success(infos);
    }
    @GetMapping(value = "/list")
    public Result<?> getCompanyList(HttpServletRequest request){
    	JSONObject params = new JSONObject();
    	params.put("state", 0);//state为0 查询未锁定的企业
    	JSONArray infos = companyService.getCompanyInfo(params);
		return ResultUtil.success(infos);
    }
    @PostMapping(value = "/add")
    public Result<?> add(HttpServletRequest request, @RequestBody Company company){
    	Object userID = request.getAttribute("userID");
    	//JSONObject params = JSONObject.parseObject(param);
    	company.setCreateUser(Long.parseLong(userID.toString()));
    	company.setCreateDate(new Date());
    	int num = companyService.insertCompany(company);
		return ResultUtil.success(num);
    }
    @PutMapping(value = "/update")
    public Result<?> update(HttpServletRequest request, @RequestBody Company company){
    	Object userID = request.getAttribute("userID");
    	company.setUpdateUser(Long.parseLong(userID.toString()));
    	company.setUpdateDate(new Date());
    	int num = companyService.updateCompany(company);
		return ResultUtil.success(num);
    }
    @PutMapping(value = "/updateState")
    public Result<?> lock(HttpServletRequest request, @RequestBody Company company){
    	Object userID = request.getAttribute("userID");
    	company.setUpdateUser(Long.parseLong(userID.toString()));
    	company.setUpdateDate(new Date());
    	int num = companyService.updateCompanyState(company);
		return ResultUtil.success(num);
    }
    @DeleteMapping(value = "/delete/{companyId}")
    public Result<?> delete(HttpServletRequest request, @PathVariable("companyId") Integer companyId){
    	int num = companyService.deleteCompany(companyId);
		return ResultUtil.success(num);
    }
}
